<?php
/**
 * ApplicationController is template for all applications running under FMS Masterplan
 * @package Core
 * @author Urban Soban
 * @copyright 2009 Urban Soban
 */

abstract class ApplicationController {
	/**
	  Contains application name. It is used for including files.
	  @var string
	*/
	protected $AppName;
	
	 /**
	 * Holds application status code.
	 * 0 means everything is in order, any other number is error code.
	 * @var int
	 */
	protected $Status = 0;
	
	/**
	 * Holds info about error.
	 * Used only if {@link ApplicationController::$Status} is not 0
	 * @var string
	 */
	protected $ErrorInfo;
	
	/**
	 * Array with list of file names to be included
	 * Application searches for file in it's own directory
	 * @var array
	 */
	protected $IncludeFiles = array();
	
	/**
	 * List of tags to be added to <head>
	 * @var array
	 */
	protected $HeadTags = array();
	
	/**
	 * List of applications, which will be used
	 * @var array
	 */
	protected $UseApps = array();
	
	/**
	 * View file name
	 * @var string
	 */
	protected $ViewFile;
	
	/**
	 * Holds Language object for given application
	 * @var Language
	 */
	protected $Language;
	
	function __construct(){
		
		// Include files
		if(count($this->IncludeFiles) > 0){
			$AppPath = strtolower($this->AppName);
			
			foreach ($this->IncludeFiles as $file){
				$FilePath = dirname(__FILE__)."/../lib/".$AppPath."/".$file;
			
				if(!file_exists($FilePath)){
					$this->Status = 1;
					$this->ErrorInfo = "Cannot find < $file > in < $FilePath >";
				}
				
				require_once($FilePath);
			}
		}
		
		// Realize applications
		if(count($this->UseApps) > 0){
			foreach ($this->UseApps as $AppN => $App){
				$AppName = $App["name"];
				$AppMethod = (isset($App["method"])) ? $App["method"] : null;
				$AppParam = (isset($App["param"])) ? $App["param"] : null;
				
				$Request = new Request($AppName, $AppMethod, $AppParam);
				$this->UseApps[$AppName] = RequestHandler::interrupt($Request, 1);
				unset($this->UseApps[$AppN]);
			}
		}
		
		$this->Language = new Language($this->AppName);
		
		// Get Smarty object
		$Smarty = ApplicationRegistry::getInstance()->get("Smarty");
		
		// If Smarty hasn't been initialized yet or application doesn't have any head tags, just end initialization
		if(!$Smarty || count($this->HeadTags) < 1){
			return true;
		}

		// Add head tags to Smarty varible and assign it back
		$SmartyRequest = new Request("smarty");
		$SmartyController = RequestHandler::interrupt($SmartyRequest, 1);
		
		foreach ($this->HeadTags as $Tag){
			$SmartyController->addHeadTag($Tag);
		}
	}
	
	/**
	 * Funtion is invoked if not run method is given to Request
	 * All FMS applications must have init() method, although it can be empty
	 */
	abstract function init();
	
	/**
	 * Returns {@link ApplicationController::$Status}
	 * @return int
	 */
	function getStatus(){
		return $this->Status;
	}
	
	/**
	 * Returns {@link ApplicationController::$ErrorInfo}
	 * @return string
	 */
	function getErrorInfo(){
		return $this->ErrorInfo;
	}
	
	/**
	 * Returns the name of assigned view file
	 * @return string
	 */
	function getView(){
		return $this->ViewFile;
	}
	
	/**
	 * Returns Language object of given application
	 * @return Language
	 */
	function getLanguage(){
		return $this->Language;
	}
}

?>